package flp.gui.view.genetic;

import flp.Layout;
import flp.solvers.ISettings;
import flp.solvers.genetic.GeneticSettings;

/**
 * Klasa wstawiana do ResultViewerForm, w celu wyswietlania postepu
 * optymalizacji.
 * 
 * @author Necro
 */
public class GeneticInfoPanel extends javax.swing.JPanel {

    private GeneticSettings settings;
    
     /**
     * Metoda inicjalizujaca nowy panel.
     */
    public GeneticInfoPanel() {
        initComponents();
    }

    /** 
     * Metoda ustawiajaca tekst w Labelce pokazujacej czas obliczen.
     */
    public void setTimeLabelText(String newText) {
        this.timerLabel.setText(newText);
    }

    /** 
     * Konstruktor tworzacy nowy Genetic Panel
     * 
     * @param settings Ustawienia z jakimi pracuje algorytm genetyczny.
     */
    public GeneticInfoPanel(GeneticSettings settings) {
        this();
        this.settings = settings;
        initData();
    }
    
    /**
     * Metoda przygotowujaca panel informacyjny (parametry algorytmu).
     * 
     */
    private void initData()
    {
        totalValueLabel.setText("" + settings.getRedrawCount());
        maxIterValueLabel.setText("" + settings.getMaxIterations());
        maxIterSCValueLabel.setText("" + settings.getStopIterations());
        populationCountValueLabel.setText("" + settings.getPopulationCount());
        epsilonValueLabel.setText("" + settings.getEpsilon());
        mutationProbabilityValueLabel.setText("" + settings.getMutationProbability());
        
        if (1 != settings.getType())
        {
            totalLabel.setVisible(false);
            totalValueLabel.setVisible(false);
            totalProgressBar.setVisible(false);
        }
        resultLabel.setVisible(false);
        resultValueLabel.setVisible(false);

        iterationProgressBar.setMinimum(0);
        iterationProgressBar.setMaximum(settings.getMaxIterations() - 1);
        iterationProgressBar.setStringPainted(true);
        iterationProgressBar.setToolTipText("Pasek postępu dla wykonywanych iteracji");
        
        totalProgressBar.setMinimum(0);
        totalProgressBar.setMaximum(settings.getRedrawCount());
        totalProgressBar.setStringPainted(true);
        totalProgressBar.setToolTipText("Pasek postępu dla cykli algorytmu");
    }

    /** 
     * Metoda aktualizujaca informacje pokazywane na panelu.
     * 
     * @param newIter Numer aktualnej iteracji.
     * @param newTopCost Najlepszy koszt.
     * @param newAvgCost Sredni koszt.
     * @param currentRedraw Numer aktualnego cyklu (tylko rozszerzony algorytm).
     */
    public void updateValues(int newIter, int newTopCost, int newAvgCost, int currentRedraw)
    {
        topCostValueLabel.setText(Integer.toString(newTopCost));
        averageCostValueLabel.setText(Integer.toString(newAvgCost));
        markIterationBar(newIter);
        markTotalBar(currentRedraw);
    }
    
    public void updateValues(int newIter, int newTopCost, int newAvgCost)
    {
        updateValues(newIter, newTopCost, newAvgCost, 1);
    }

    /**
     * Metoda wywolywana po zakonczeniu obliczen dla danego przypadku
     * wyswietla dla niego rozwiazanie problemu.
     * 
     * @param bestLayout Najlepszy Layout znaleziony podczas optymalizacji.
     */
    public void showResult(Layout bestLayout) {
        topCostLabel.setEnabled(false);
        topCostValueLabel.setEnabled(false);
        averageCostLabel.setEnabled(false);
        averageCostValueLabel.setEnabled(false);
        resultValueLabel.setText(""+bestLayout.toString2()+"    cost: "+topCostValueLabel.getText());
        resultLabel.setVisible(true);
        resultValueLabel.setVisible(true);
    }

    /**
     * Metoda zaznaczajaca postep na pasku postepu.
     * 
     * @param tryVal Wartosc do zaznaczenia.
     */
    private void markIterationBar(int iteration) {
        iterationProgressBar.setValue(iteration);
        iterationProgressBar.setString(String.format("%d / %d", iteration, settings.getMaxIterations()));
    }
    
    private void markTotalBar(int total)
    {
        totalProgressBar.setValue(total);
        totalProgressBar.setString(String.format("%d / %d", total, settings.getRedrawCount()));
    }

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        titleLabel = new javax.swing.JLabel();
        titlePrefixLabel = new javax.swing.JLabel();
        timerLabel = new javax.swing.JLabel();
        resultValueLabel = new javax.swing.JLabel();
        resultLabel = new javax.swing.JLabel();
        iterationProgressBar = new javax.swing.JProgressBar();
        geneticSettingsPanel = new javax.swing.JPanel();
        maxIterValueLabel = new javax.swing.JLabel();
        maxIterLabel = new javax.swing.JLabel();
        maxSCIterLabel = new javax.swing.JLabel();
        maxIterSCValueLabel = new javax.swing.JLabel();
        mutationProbabilityLabel = new javax.swing.JLabel();
        mutationProbabilityValueLabel = new javax.swing.JLabel();
        epsilonLabel = new javax.swing.JLabel();
        epsilonValueLabel = new javax.swing.JLabel();
        populationCountValueLabel = new javax.swing.JLabel();
        populationCountLabel = new javax.swing.JLabel();
        totalLabel = new javax.swing.JLabel();
        totalValueLabel = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        topCostLabel = new javax.swing.JLabel();
        topCostValueLabel = new javax.swing.JLabel();
        averageCostLabel = new javax.swing.JLabel();
        averageCostValueLabel = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        totalProgressBar = new javax.swing.JProgressBar();

        titleLabel.setFont(new java.awt.Font("Tahoma", 1, 14));
        titleLabel.setForeground(new java.awt.Color(0, 102, 0));
        titleLabel.setText("ALGORYTM GENETYCZNY");

        titlePrefixLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        titlePrefixLabel.setText("Do optymalizacji zastosowano:");

        timerLabel.setFont(new java.awt.Font("Tahoma", 1, 12));
        timerLabel.setForeground(new java.awt.Color(0, 0, 102));
        timerLabel.setText("00:00");

        resultValueLabel.setFont(new java.awt.Font("Tahoma", 1, 11));
        resultValueLabel.setForeground(new java.awt.Color(0, 0, 102));
        resultValueLabel.setText("przykladowy wynik");

        resultLabel.setFont(new java.awt.Font("Tahoma", 1, 11));
        resultLabel.setText("WYNIK:");

        geneticSettingsPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Ustawienia algorytmu"));

        maxIterValueLabel.setFont(new java.awt.Font("Tahoma", 1, 11));
        maxIterValueLabel.setText("0");

        maxIterLabel.setText("Maksymalna ilość iteracji:");

        maxSCIterLabel.setText("Maksymalna ilość iteracji ze spełnionym warunkiem stopu:");

        maxIterSCValueLabel.setFont(new java.awt.Font("Tahoma", 1, 11));
        maxIterSCValueLabel.setText("0");

        mutationProbabilityLabel.setText("Prawdopodobieństwo mutacji (%):");

        mutationProbabilityValueLabel.setFont(new java.awt.Font("Tahoma", 1, 11));
        mutationProbabilityValueLabel.setText("0");

        epsilonLabel.setText("Epsilon dla warunku stopu:");

        epsilonValueLabel.setFont(new java.awt.Font("Tahoma", 1, 11));
        epsilonValueLabel.setText("0");

        populationCountValueLabel.setFont(new java.awt.Font("Tahoma", 1, 11));
        populationCountValueLabel.setText("0");

        populationCountLabel.setText("Liczność populacji:");

        totalLabel.setText("Liczba cykli algorytmu:");

        totalValueLabel.setFont(new java.awt.Font("Tahoma", 1, 11));
        totalValueLabel.setText("0");

        javax.swing.GroupLayout geneticSettingsPanelLayout = new javax.swing.GroupLayout(geneticSettingsPanel);
        geneticSettingsPanel.setLayout(geneticSettingsPanelLayout);
        geneticSettingsPanelLayout.setHorizontalGroup(
            geneticSettingsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(geneticSettingsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(geneticSettingsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(geneticSettingsPanelLayout.createSequentialGroup()
                        .addComponent(maxIterLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(maxIterValueLabel))
                    .addGroup(geneticSettingsPanelLayout.createSequentialGroup()
                        .addComponent(maxSCIterLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(maxIterSCValueLabel))
                    .addGroup(geneticSettingsPanelLayout.createSequentialGroup()
                        .addComponent(epsilonLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(epsilonValueLabel))
                    .addGroup(geneticSettingsPanelLayout.createSequentialGroup()
                        .addComponent(populationCountLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(populationCountValueLabel))
                    .addGroup(geneticSettingsPanelLayout.createSequentialGroup()
                        .addComponent(mutationProbabilityLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(mutationProbabilityValueLabel))
                    .addGroup(geneticSettingsPanelLayout.createSequentialGroup()
                        .addComponent(totalLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(totalValueLabel)))
                .addContainerGap(36, Short.MAX_VALUE))
        );
        geneticSettingsPanelLayout.setVerticalGroup(
            geneticSettingsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(geneticSettingsPanelLayout.createSequentialGroup()
                .addGroup(geneticSettingsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(totalLabel)
                    .addComponent(totalValueLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(geneticSettingsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(maxIterLabel)
                    .addComponent(maxIterValueLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(geneticSettingsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(maxSCIterLabel)
                    .addComponent(maxIterSCValueLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(geneticSettingsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(epsilonLabel)
                    .addComponent(epsilonValueLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(geneticSettingsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(populationCountLabel)
                    .addComponent(populationCountValueLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(geneticSettingsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(mutationProbabilityLabel)
                    .addComponent(mutationProbabilityValueLabel))
                .addGap(22, 22, 22))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Monitor wartości"));

        topCostLabel.setText("Koszt dla najlepszego osobnika:");

        topCostValueLabel.setFont(new java.awt.Font("Tahoma", 1, 11));
        topCostValueLabel.setText("0");

        averageCostLabel.setText("Średni koszt osobnika :");

        averageCostValueLabel.setFont(new java.awt.Font("Tahoma", 1, 11));
        averageCostValueLabel.setText("0");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(topCostLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(topCostValueLabel))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(averageCostLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(averageCostValueLabel)))
                .addContainerGap(25, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(topCostLabel)
                    .addComponent(topCostValueLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(averageCostLabel)
                    .addComponent(averageCostValueLabel))
                .addContainerGap(82, Short.MAX_VALUE))
        );

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel1.setForeground(new java.awt.Color(0, 0, 102));
        jLabel1.setText("Czas trwania obliczeń:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(iterationProgressBar, javax.swing.GroupLayout.DEFAULT_SIZE, 572, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(resultLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(resultValueLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 524, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(titlePrefixLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(titleLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 201, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 58, Short.MAX_VALUE)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(timerLabel))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(geneticSettingsPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(totalProgressBar, javax.swing.GroupLayout.DEFAULT_SIZE, 572, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(titlePrefixLabel)
                    .addComponent(titleLabel)
                    .addComponent(timerLabel)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(totalProgressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(iterationProgressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(geneticSettingsPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 146, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(resultLabel)
                    .addComponent(resultValueLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel averageCostLabel;
    private javax.swing.JLabel averageCostValueLabel;
    private javax.swing.JLabel epsilonLabel;
    private javax.swing.JLabel epsilonValueLabel;
    private javax.swing.JPanel geneticSettingsPanel;
    private javax.swing.JProgressBar iterationProgressBar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JLabel maxIterLabel;
    private javax.swing.JLabel maxIterSCValueLabel;
    private javax.swing.JLabel maxIterValueLabel;
    private javax.swing.JLabel maxSCIterLabel;
    private javax.swing.JLabel mutationProbabilityLabel;
    private javax.swing.JLabel mutationProbabilityValueLabel;
    private javax.swing.JLabel populationCountLabel;
    private javax.swing.JLabel populationCountValueLabel;
    private javax.swing.JLabel resultLabel;
    private javax.swing.JLabel resultValueLabel;
    private javax.swing.JLabel timerLabel;
    private javax.swing.JLabel titleLabel;
    private javax.swing.JLabel titlePrefixLabel;
    private javax.swing.JLabel topCostLabel;
    private javax.swing.JLabel topCostValueLabel;
    private javax.swing.JLabel totalLabel;
    private javax.swing.JProgressBar totalProgressBar;
    private javax.swing.JLabel totalValueLabel;
    // End of variables declaration//GEN-END:variables
}
